<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>后台首页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <link rel="stylesheet" href="${ctxPath}/static/manage/layui/css/layui.css" media="all">
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
</head>
<body>
    <div class="x-nav">
         <span class="nav-breadcrumb">
             <a><cite>后台管理></cite></a>
             <a><cite>网站首页></cite></a>
             <a><cite>首页列表</cite></a>
         </span>
    </div>
    <div class="x-body">
        <blockquote class="layui-elem-quote quoteBox">
            <div class="layui-inline">
                <a class="layui-btn addNews_btn">添加新闻图片</a>
            </div>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </blockquote>

        <input type="hidden" id="articleArray" value=${articleArray}>

        <table id="homeTable" lay-filter="homeTable"></table>

    </div>

</body>
</html>

<script type="text/javascript">
    layui.use(['jquery', 'table', 'layer', 'util'], function(){
        var $ = layui.$, table = layui.table, layer = layui.layer, util = layui.util;

        table.render({
            elem: '#homeTable'
            ,height: 500
            ,url: 'home/homeTable' //数据接口
            ,page: true //开启分页
            ,limit: 10
            ,limits : [10]
            ,cols: [[ //表头
                { checkbox: true, fixed: true },
                { field: 'id', title: '序号', width:100, sort: true, fixed: true },
                { field: 'modularName', title: '模块', width:150, sort: true },
                { field: 'type', title: '所属类型', width:150, sort: true, templet: function(d){return  d.type == '1' ? "新闻图片":"下方图片";}},
                { field: 'sort', title: '显示顺序', width:150, sort: true },
                { field: 'pictureUrl', title: '新闻图片', width:150, templet:'<div><img src="/{{d.pictureUrl}}"></div>'},
                { field: 'uploadTime', title: '上传时间',width:180, sort: true,templet :function (d){return util.toDateString(d.uploadTime);}},
                { field: 'articleTitle', title: '对应文章' },
                { field: 'state', title: '是否显示', width:100, event: 'state', templet: function(d){
                        var html = ['<input type="checkbox" name="state" lay-skin="switch" lay-text="是|否"'];
                        html.push(d.state == 1 ? ' checked' : '');
                        html.push('/>');
                        return html.join('');
                    }
                },
                { field: 'tempId', title: '操作', width:180, templet: function(d){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-edit"></i>编辑</a>' +
                            '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-delete"></i>移除</a>';
                    }
                }
            ]]
        });

        $('.addNews_btn').on('click', function(){
            layer.open({
                title : "添加首页内容",
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['700px', '500px'],
                content: "/manage/home/toHomeAdd",
                success: function (layero, index) {
                    // 动态生成文章 下拉框
                    var body = layui.layer.getChildFrame('body', index);
                    var articleArray = $.parseJSON($("#articleArray").val());
                    $.each(articleArray[0],function(index,item){
                        body.find("#articleId").append("<option value=" + item.id + ">"+ item.title + "</option>");
                    });
                }
            });
        });

        // 编辑
        function editHome(rowData) {
            var index = layer.open({
                title : "编辑首页内容",
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['700px', '500px'],
                content: "/manage/home/toHomeUpdate",
                success: function (layero, index) {
                    var body = layui.layer.getChildFrame('body', index);
                    // 动态生成文章 下拉框
                    var articleArray = $.parseJSON($("#articleArray").val());
                    $.each(articleArray[0],function(index,item){
                        body.find("#articleId").append("<option value=" + item.id + ">"+ item.title + "</option>");
                    });
                    body.find(".sortSelect select").val(rowData.sort);
                    body.find(".typeSelect select").val(rowData.type);
                    body.find(".articleSelect select").val(rowData.articleId);
                    body.find("#id").val(rowData.id);
                    body.find("#pictureId").val(rowData.pictureId);
                    body.find(".thumbImg").attr("src", "/"+rowData.pictureUrl);
                }
            });
        }

        // 监听工具条
        table.on('tool(homeTable)', function (obj) {
            // data当前行数据， lay-event或event 对应的值，当前行tr的DOM对象
            var data = obj.data, event = obj.event, tr = obj.tr;
            console.log(data);
            switch(event){
                case "edit":
                    editHome(data);
                    break;
                case "state":
                    var stateValue = tr.find("input[name='state']").prop('checked') ? 1 : 2;
                    $.ajax({
                        type: 'post',
                        url: '/manage/home/updateHomeState',
                        data: {'id': data.id, 'stateValue': stateValue},
                        success:function(result){
                            alert(result);
                            location.replace(location.href);                // 刷新
                        }
                    });
                    break;
                case "del":
                    layer.confirm('真的删除行么？', function(index){
                        layer.close(index);
                        $.ajax({
                            type: 'post',
                            url: '/manage/home/deleteHome',
                            data: {'id': data.id},
                            success:function(result){
                                alert(result);
                                window.location.reload();      // 刷新
                            }
                        });
                    });
                    break;
            }
        });
    });

</script>
